﻿using Microsoft.AspNetCore.Mvc;
using DGZImp.WebApi.Services;
using System.Threading.Tasks;
using System;

[ApiController]
[Route("api/[controller]/[action]")]
public class EmailController : ControllerBase
{
    private readonly EmailService _emailService;

    public EmailController(EmailService emailService)
    {
        _emailService = emailService;
    }

    [HttpPost]
    public async Task<IActionResult> SendEmail([FromBody] EmailRequest request)
    {
        try
        {
            await _emailService.SendEmailAsync(
                request.ToEmail,
                request.Subject,
                request.Body
            );
            return Ok(new { Success = true, Message = "邮件发送成功！" });
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { Success = false, Message = $"邮件发送失败：{ex.Message}" });
        }
    }
}

public class EmailRequest
{
    public string ToEmail { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
}
